package com.demo.shardingjdbc.controller;

import com.demo.shardingjdbc.entity.User;
import com.demo.shardingjdbc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author hcj
 * @title 用户(User)表控制层
 * @createDate 2018-11-22
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/page")
    public Object page(User record, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        // 在slaver-mysql中查询，500万的数据，在没有索引的情况下，跟在一个table中查询的效率基本一致
        return userService.queryPage(record, pageNum, pageSize);
    }

    @GetMapping("/selectSelective")
    public List<User> selectSelective(User record) {
        return userService.selectSelective(record);
    }

    /**
     * 测试返回map
     * @param record
     * @author huangcj
     * @date 2020-10-30 19:12
     */
    @GetMapping("/selectSelectiveAndReturnMap")
    public List<Map> selectSelectiveByMap(User record) {
        return userService.selectSelectiveAndReturnMap(record);
    }

    @GetMapping("/testLeftJoin")
    public List<Map> testLeftJoin(User record) {
        return userService.testLeftJoin(record);
    }

    /**
     * 联合查询【shardingJdbc不支持 union, 也不影响它的强大功能，完全可以用代码来实现union】
     * @param record
     * @return
     */
    @GetMapping("/testUnion")
    public List<Map> testUnion(User record) {
        return userService.testUnion(record);
    }


    @PostMapping("/save")
    public Object save(@RequestBody User record) {
        return userService.insertSelective(record);
    }

    @PostMapping("/update")
    public Object update(@RequestBody User record) {
        return userService.updateSelective(record);
    }

    @PostMapping("/delete/{userId}")
    public Object delete(@PathVariable Long userId) {
        return userService.deleteById(userId);
    }


}